<?php
/**
 *
 * User: lisgroup
 * Date: 2017/11/10
 * Time: 19:33
 */

require_once __DIR__.'/../init.php';

function checkId($idcard)
{

    $len = strlen($idcard);
    if ($len != 18) {
        return false;
    }

    $a = str_split($idcard, 1);
    $w = array(7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2);

    $c = array(1, 0, 'x', 9, 8, 7, 6, 5, 4, 3, 2);

    $sum = 0;
    for ($i = 0; $i < 17; $i++) {
        $sum += $a[$i] * $w[$i];
    }

    $r = $sum % 11;
    $res = $c[$r];

    //echo '校验位是：'.$res;

    if (strtoupper($res) == $a[17]) {
        return true;
    } else {
        return false;
    }
}

$config = array(
    'DB_HOST' => 'localhost',
    'DB_PORT' => '3306',
    'DB_USER' => 'root',
    'DB_PASS' => 'root',
    'DB_NAME' => 'temp',
    'DB_CHARSET' => 'utf8'
);
$mysqli = new \Apps\lib\MysqlQuery($config);

$sql = "SELECT * FROM `bankcard3_verify` WHERE res = 1 LIMIT 10000;";
$res = $mysqli->getAll($sql);
//var_dump($res);

foreach ($res as $re) {
    if (!checkId($re['idcard'])) {
        echo '不匹配的id：'.$re['id'].';  身份证号码：'.$re['idcard'].'<br>';
    }
}

